package main

import (
	"fmt"
	"github.com/lionsoul2014/ip2region/binding/golang/xdb"
	"google.golang.org/protobuf/runtime/protoimpl"
	"strings"
)

type WorksInfo struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	Nick     string   `protobuf:"bytes,1,opt,name=Nick,proto3" json:"Nick,omitempty"`
	WechatId string   `protobuf:"bytes,2,opt,name=WechatId,proto3" json:"WechatId,omitempty"`
	Phone    int64    `protobuf:"varint,3,opt,name=Phone,proto3" json:"Phone,omitempty"`
	Page     []string `protobuf:"bytes,4,rep,name=Page,proto3" json:"Page,omitempty"`
}

type CSpaceWorksUpload struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	WorksInfo *WorksInfo `protobuf:"bytes,1,opt,name=worksInfo,proto3" json:"worksInfo,omitempty"`
}

func main() {
	//var fileUrl = "https://novel-1308380417.cos.ap-nanjing.myqcloud.com/dir/ip2region.xdb"
	//resp, err := http.Get(fileUrl)
	//if err != nil {
	//	panic(err)
	//}
	//fmt.Println(resp.Status)
	//defer resp.Body.Close()
	//
	//data, err := ioutil.ReadAll(resp.Body)
	//if err != nil {
	//	panic(err)
	//}
	//searcher, err := xdb.NewWithBuffer(data)
	//if err != nil {
	//	fmt.Printf("failed to create searcher: %s\n", err.Error())
	//	return
	//}
	var dbPath = "./ip2region.xdb"
	searcher, err := xdb.NewWithFileOnly(dbPath)
	if err != nil {
		fmt.Printf("failed to create searcher: %s\n", err.Error())
		return
	}

	defer searcher.Close()

	//// do the search
	var ip = "36.27.95.243"
	var region string
	//

	result, err := searcher.SearchByStr(ip)
	if err != nil {
		fmt.Printf("failed to SearchIP(%s): %s\n", ip, err)
		return
	}
	//fmt.Println(result)
	//result = strings.ReplaceAll(result, "|0", "")
	fmt.Println(result)

	tagList := strings.Split(result, "|")

	if len(tagList) > 0 {
		if tagList[0] == "中国" {
			if len(tagList) >= 4 {
				region = tagList[3]
			}
		} else {
			region = tagList[0]
		}
	}
	cc := "%sccdd"
	fmt.Println(fmt.Sprintf(cc, "wqu,"))
	fmt.Printf("{region: %s}\n", region)

	data := &struct{}{}
	if data == nil {
		fmt.Println("111111111")
	} else {
		fmt.Println("2222222")
	}
	fmt.Println(data)
	type TestStruct struct {
		ids  []int
		name string
	}

	cd := &CSpaceWorksUpload{}
	fmt.Println(cd.WorksInfo)
	fmt.Println(cd.WorksInfo.Nick)

	type acd struct {
		ids []int32
	}

	accd := &acd{}
	fmt.Println(accd.ids)
	accd.ids[0] = 1
	fmt.Println(accd.ids[0])
	// 备注：并发使用，每个 goroutine 需要创建一个独立的 searcher 对象。
	//defer func() {
	//	err := recover()
	//	fmt.Println(err)
	//}()
	//handleError()
	//fmt.Println("1111111111111")
}
